package cn.swufe.edu.stu.hw_tools.Time;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.swufe.edu.stu.hw_tools.R;

public class SetTimeActivity extends AppCompatActivity {
    private TextView timerTextView;
    private EditText timeEditText;
    private Button startButton, pauseButton, resetButton,backButton;
    private long timeLeft = 0;
    private boolean isRunning = false;
    private Handler handler = new Handler();
    private Runnable runnable;
    private boolean isPasued = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_time);

        timerTextView = findViewById(R.id.timerTextView2);
        timeEditText = findViewById(R.id.timeEditText2);
        startButton = findViewById(R.id.startButton2);
        pauseButton = findViewById(R.id.pauseButton2);
        resetButton = findViewById(R.id.resetButton2);
        backButton = findViewById(R.id.backbutton2);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pauseTimer();
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTimer();
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(2,intent);
                finish();
            }
        });

        runnable = new Runnable() {
            @Override
            public void run() {
                if (timeLeft > 0) {
                    timeLeft -= 10;
                    updateTimerText();
                    handler.postDelayed(this, 100);
                }else{
                    Toast.makeText(SetTimeActivity.this, "倒计时结束！", Toast.LENGTH_SHORT).show();
                    resetTimer();
                }
            }
        };
    }

    private void startTimer() {
        if(!isPasued){
            try {
                timeLeft = Long.parseLong(timeEditText.getText().toString()) * 100;
            } catch (NumberFormatException e) {
                timeLeft = 0;
                Toast.makeText(this, "(╬ Ò ‸ Ó)说清楚，倒计时多久？", Toast.LENGTH_SHORT).show();
            }
            isRunning = true;
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable, 100);
        }else{
            isRunning = true;
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable, 100);
        }
        timeEditText.setText("");
    }

    private void pauseTimer() {
        isRunning = false;
        isPasued = true;
        handler.removeCallbacks(runnable);
    }

    private void resetTimer() {
        pauseTimer();
        timeLeft = 0;
        isPasued = false;
        updateTimerText();
        timeEditText.setText("");
    }

    private void updateTimerText() {
        long seconds = timeLeft / 100;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 60;
        timerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
    }



}